package org.xserver.disruptor.core;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventTranslator;
/**
 * The <code>DefaultEventFactory</code> to getEventFactory and getEventTranslator 
 * very important
 * @author idoldefine
 */
public class DefaultEventFactory {
	private final static Logger logger = LoggerFactory.getLogger(DefaultEventFactory.class);
    public static <T> EventFactory<T> getEventFactory(final Class<T> eventType) {
        return new EventFactory<T>() {
            public T newInstance() {
                try {
                    return eventType.newInstance();
                } catch (InstantiationException e) {
                	logger.error("",e);
                    return null;
                } catch (IllegalAccessException e) {
                	logger.error("",e);
                    return null;
                }
            }
        };
    }

    public static <T> EventTranslator<T> getEventTranslator(final T t) {
        return new EventTranslator<T>() {
            public void translateTo(T event, long sequence) {
            	try {
            		//event = (T) CloneUtil.clone(t);
            		//event =  t;
                    BeanUtils.copyProperties(event,t);
                } catch (Exception e) {
                	logger.error("",e);
                }
            }
        };
    }
}
